package com.example.aabb;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {






    private NoScrollViewPager nsvpMain;
    private LinearLayout lin;
    private LinearLayout llHome;
    private ImageView ivHome;
    private TextView tvHome;
    private LinearLayout llMaterial;
    private ImageView ivMaterial;
    private TextView tvMaterial;
    private LinearLayout llList;
    private ImageView ivList;
    private TextView tvList;
    private LinearLayout llMine;
    private ImageView ivMine;
    private TextView tvMine;



    /*相关页面*/
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
    private ArrayList<Fragment> fragmentList;
//    private FragmentTabAdapter tabAdapter;

    HomeFragment homeFragment;//微信
    FreeFragment freeFragment;//通讯录
    ListFragment listFragment;//发现
    MineFragment mineFragment;//我的


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nsvpMain = (NoScrollViewPager) findViewById(R.id.nsvp_main);
        lin = (LinearLayout) findViewById(R.id.lin);
        llHome = (LinearLayout) findViewById(R.id.ll_home);
        ivHome = (ImageView) findViewById(R.id.iv_home);
        tvHome = (TextView) findViewById(R.id.tv_home);
        llMaterial = (LinearLayout) findViewById(R.id.ll_material);
        ivMaterial = (ImageView) findViewById(R.id.iv_material);
        tvMaterial = (TextView) findViewById(R.id.tv_material);
        llList = (LinearLayout) findViewById(R.id.ll_list);
        ivList = (ImageView) findViewById(R.id.iv_list);
        tvList = (TextView) findViewById(R.id.tv_list);
        llMine = (LinearLayout) findViewById(R.id.ll_mine);
        ivMine = (ImageView) findViewById(R.id.iv_mine);
        tvMine = (TextView) findViewById(R.id.tv_mine);

        llHome.setOnClickListener(this);
        llMaterial.setOnClickListener(this);
        llList.setOnClickListener(this);
        llMine.setOnClickListener(this);

        homeFragment = new HomeFragment();
        freeFragment = new FreeFragment();
        listFragment = new ListFragment();
        mineFragment = new MineFragment();

        fragmentList = new ArrayList<>();
        fragmentList.add(homeFragment);
        fragmentList.add(freeFragment);
        fragmentList.add(listFragment);
        fragmentList.add(mineFragment);
//        tabAdapter = new FragmentTabAdapter(this, fragments, R.id.fl_layout);

        setBottomTab(0);

        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(
                getSupportFragmentManager(),fragmentList);
        nsvpMain.setAdapter(mMyFragmentPagerAdapter);
        nsvpMain.setOffscreenPageLimit(5);
        nsvpMain.setCurrentItem(0);
        nsvpMain.setOnPageChangeListener(new MyOnPageChangeListener());
        nsvpMain.setNoScroll(true);
    }

    /**
     * fragment适配器
     */
    public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {

        ArrayList<Fragment> list;
        private final String[] TITLES = {""};

        public MyFragmentPagerAdapter(FragmentManager fm,
                                      ArrayList<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return list.size();
        }


        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }

        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }

    }

    /**
     * 页面切换监听
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {


        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int arg0) {
            setBottomTab(arg0);
        }
    }

    private void setBottomTab(int position){
        tvHome.setTextColor(getResources().getColor(R.color.color_40424B));
        tvMaterial.setTextColor(getResources().getColor(R.color.color_40424B));
        tvList.setTextColor(getResources().getColor(R.color.color_40424B));
        tvMine.setTextColor(getResources().getColor(R.color.color_40424B));

        ivHome.setImageResource(R.mipmap.a);
        ivMaterial.setImageResource(R.mipmap.b);
        ivList.setImageResource(R.mipmap.c);
        ivMine.setImageResource(R.mipmap.d);

        switch (position) {
            case 0:
                tvHome.setTextColor(getResources().getColor(R.color.color_FD2650));
                ivHome.setImageResource(R.mipmap.a1);
                break;
            case 1:
                tvMaterial.setTextColor(getResources().getColor(R.color.color_FD2650));
                ivMaterial.setImageResource(R.mipmap.b1);
                break;
            case 2:
                tvList.setTextColor(getResources().getColor(R.color.color_FD2650));
                ivList.setImageResource(R.mipmap.c1);
                break;
            case 3:
                tvMine.setTextColor(getResources().getColor(R.color.color_FD2650));
                ivMine.setImageResource(R.mipmap.d1);
                break;
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_home:
                nsvpMain.setCurrentItem(0, false);
                break;
            case R.id.ll_material:
                nsvpMain.setCurrentItem(1, false);
                break;
            case R.id.ll_list:
                nsvpMain.setCurrentItem(2, false);
                break;
            case R.id.ll_mine:
                nsvpMain.setCurrentItem(3, false);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
